<template>
  <div class="comment">

    <!-- 咨询 -->
    <div class="search-container">
      <div class="search-box">
          <i class="iconfont" @click="fetchData(1)">&#xe63c;</i>
        <span class="reset-btn" @click="resetSearch">重置搜索</span>
        <input type="text" class="search-input" placeholder="请输入咨询进行搜索" v-model="queryParams.consult" @keyup.enter="fetchData(1)">
      </div>
      <div class="search-btn" @click="addConsult">
        我要咨询
      </div>
    </div>

    <!-- 评价列表 -->
    <ul class="comment-list">
      <li v-for="(item,index) in consultList" :key="index" class="comment-item">
        <div class="img-box">
          <img :src="item.avatar" alt="" style="object-fit: cover">
        </div>
        <div class="comment-content-wrapper">
          <p>
            <span class="name">{{ item.name }}</span>
          </p>
          <h3 class="title" v-html="item.consult"></h3>
          <p class="content" v-if="item.reply == null" style="background-color:#f4f5f7;">
            {{ `系统消息：讲师还未回答~` }}
          </p>
          <p class="content" v-if="item.reply != null" style="background-color:#f4f5f7;">
            {{ `讲师回复：` + item.reply }}
          </p>
          <p>
            <span @click="doLike(item.consultId)" :class="LikeList.indexOf(item.consultId) === -1 ? 'like' : 'like-click'">
              <i class="iconfont">&#xe60c;</i>
              {{ item.like }}
            </span>
            <span class="time">时间：{{ item.gmtCreate }}</span>
          </p>
        </div>
      </li>
    </ul>

    <div v-if="consultList.length === 0 || consultList === null" class="list-empty">
      <p>暂无数据</p>
    </div>

    <!-- 分页 -->
    <el-pagination
        :current-page="page"
        :page-size="limit"
        :total="total"
        style="padding: 30px 0; text-align: center;"
        layout="total, prev, pager, next, jumper"
        @current-change="fetchData"
    />

    <!-- 新增咨询表单 -->
    <ConsultForm
        ref="consultForm"
        :course-id="this.$route.params.id"
        @onSaveSuccess="refreshList"/>

  </div>
</template>
<script>
import { getConsultPageList } from '@/api/consult'
import {doConsultLike, getConsultLikeList} from '@/api/like'
import ConsultForm from '@/pages/course-detail/consult-form'
import { mapGetters } from 'vuex';

export default {
  data () {
    return {
      total: 0, // 总记录数
      page: 1, // 页码
      limit: 6, // 每页记录数
      consultList: [], // 咨询列表
      likeParams:{
        userId: this.userId,
        consultId: ''
      },
      queryParams: {
        courseId: '', // 课程id
        consult: '', //咨询内容
      },
      LikeList:[]
    }
  },
  created () {
    this.fetchData()
  },
  methods: {
    fetchData (page = 1) {
      // 当点击分页组件的切换按钮的时候，会传输一个当前页码的参数page
      // 解决分页无效问题
      this.page = page
      this.queryParams.courseId = this.$route.params.id
      this.getLikeList()

      getConsultPageList(this.page, this.limit, this.queryParams).then(result => {
        let {code, data} = result
        if (code === 20000) {
          this.searchLight(data.rows)
          this.total = data.total
        }
      })
    },
    addConsult(){
      //判断是否登录
      if (this.userId === ''){
        this.$message.error("请先登录系统！")
        return;
      }
      this.$refs.consultForm.open()
    },

    //点赞/取消赞
    doLike(consultId){
      //判断是否登录
      if (this.userId === ''){
        this.$message.error("请先登录系统！")
        return;
      }

      this.likeParams.userId = this.userId
      this.likeParams.consultId = consultId
      doConsultLike(this.$qs.stringify(this.likeParams)).then(result => {
        let {code, message} = result
        if (code === 20000) {
          this.refreshList(this.page)
          this.$message({
            type: 'success',
            message: message
          })
        }
      })
    },

    //刷新数据
    refreshList(page){
      this.fetchData(page)
    },

    //重置搜索
    resetSearch(){
      this.queryParams.consult = ''
      this.refreshList(1)
    },

    //获取用户点赞列表
    getLikeList(){
      // 判断是否登录
      // 无登录
      if (this.userId === ''){
        return;
      }

      getConsultLikeList(this.userId).then(result => {
        let { code, data } = result
        if (code === 20000){
          this.LikeList = data.items

        }
      })
    },

    searchLight(list){
      list.map((item, index) => {
        if (this.queryParams.consult && this.queryParams.consult.length > 0){
          // 匹配关键字正则
          let replaceReg = new RegExp(this.queryParams.consult, 'gi')
          // 高亮替换v-html值
          let replaceString = '<span class="consult-search-text">$&</span>'
          // 开始替换
          list[index].consult = item.consult.replace(replaceReg, replaceString)
        }
      })
      this.consultList = ''
      this.consultList = list
    },

  },
  computed: {
    ...mapGetters({
      'userId': 'id'
    })
  },
  components:{
    ConsultForm
  }
}
</script>
<style lang="stylus" scoped>
  .comment
    .score-overview
      padding: 24px 32px;
      display: flex;
      align-items: center;
      background-color: #fff;
      box-shadow: 0 0 8px 4px rgba(7,17,27,0.1);
      border-radius: 12px;

      .star-box
        flex: 1;
        & > span
          display: inline-block;
          vertical-align:  middle;
          line-height: 24px;
          &.title
            width: 40px;
            text-align: center;
            color: #1c1f21;
            font-size: 14px;
            line-height: 16px;
            font-weight: 700;
          &.score
            padding-left: 5px;
            font-size: 32px;
            color: #4d555d;
            font-weight: 700;
        >>> .star
          margin-left: 10px;
          vertical-align: middle;
          .star-item
            margin-right: 3px;
      .score-box
        margin-left: 50px;
        & > span
          display: inline-block;
          vertical-align:  middle;
          line-height: 24px;
          font-size: 14px;
          &.score
            padding-left: 12px;
            font-size: 26px;
            color: #f90;
            font-weight: 700;
    .comment-list
      margin-top: 24px;
      .comment-item
        margin-bottom: 8px;
        padding: 24px; 36px;
        display: flex;
        align-items: flex-start;
        background-color: #fff;
        box-shadow: 0 0 8px 4px rgba(7,17,27,.1);
        border-radius: 12px;
        .img-box
          margin-right: 20px;
          flex: 0 0 48px;
          width: 48px;
          height: 48px;
          & > img
            display: block;
            width: 100%;
            height: 100%;
            border-radius: 50%;
            background-color: #eee;
        .comment-content-wrapper
          flex: 1;
          & > p
            font-size: 14px;
            color: #14191e;
            line-height: 24px;
            &.content
              font-size 13px
              margin-top: 0px;
              margin-bottom: 8px;
              line-height: 28px;
            .name
              color: #787d82;
              font-weight: 700;
            .score, .time
              float: right;
            .time
              color: #b5b9bc;
              font-size: 12px;
            .like
              padding: 7px 15px;
              border-radius: 18px;
              background-color: rgba(84,92,99,0.1);
              color: #545c63;
              cursor: pointer;
            .like-click
              padding: 7px 15px;
              border-radius: 18px;
              background-color: rgba(240,20,20,.1);
              color: #f01414;
              cursor: pointer;
  .title
    line-height: 40px;
    font-size: 16px;
    color: #07111b;
    font-weight: 700;
  .search-container
    display: flex;
    align-items: center;
    height: 50px;
    .search-box
      flex: 1;
      position: relative;
      margin-right: 20px;
      padding: 0 60px 0 16px;
      overflow: hidden;
      border: 1px solid #b2b8bd;
      border-radius: 8px;
      box-sizing: border-box;
      background: #fff;
      .iconfont
        position: absolute;
        right: 20px;
        cursor: pointer;
        top: 50%;
        transform: translateY(-50%);
        font-size: 30px;
        color: #9199a1;
        background-color: #fffff;
      .search-input
        width: 100%;
        height: 48px;
        background-color: transparent;
        font-size: 16px;
        line-height: 24px;
        outline: none;
    .search-btn
      flex: 0 0 138px;
      width: 138px;
      height: 48px;
      text-align: center;
      line-height: 48px;
      background-color: #f20d0d;
      border-radius: 24px;
      color: #fff;
      cursor: pointer;
  .list-empty
    padding: 10px 0;
    text-align: center;
  .reset-btn
    display: inline-block;
    position: absolute;
    top: 0;
    right: 25px;
    margin-top 18px
    font-size: 14px;
    cursor: pointer;
    margin-right 30px
</style>
